trigger:
  branches:
    include:
    - "main"

stages:
- stage: check
  dependsOn: []
  pool: "envoy-x64-large"
  jobs:
  - job: build_and_format
    displayName: "do_ci.sh"
    dependsOn: []
    strategy:
      maxParallel: 2
      matrix:
        build:
          CI_TARGET: "build"
        format:
          CI_TARGET: "check_format"
    timeoutInMinutes: 120
    steps:
    - template: bazel.yml
      parameters:
        ciTarget: $(CI_TARGET)

- stage: test
  dependsOn: ["check"]
  pool: "envoy-x64-large"
  jobs:
  - job: test_and_benchmark
    displayName: "do_ci.sh"
    strategy:
      # Both test and benchmark need dedicated resources for stability.
      maxParallel: 1
      matrix:
        test:
          CI_TARGET: "test"
        benchmark:
          CI_TARGET: "benchmark_with_own_binaries"
    timeoutInMinutes: 120
    steps:
    - template: bazel.yml
      parameters:
        ciTarget: $(CI_TARGET)

- stage: test_gcc
  dependsOn: ["check"]
  pool: "envoy-x64-large"
  jobs:
  - job: test_gcc
    displayName: "do_ci.sh"
    strategy:
      maxParallel: 1
      matrix:
        test_gcc:
          CI_TARGET: "test_gcc"
    timeoutInMinutes: 120
    steps:
    - template: bazel.yml
      parameters:
        ciTarget: $(CI_TARGET)

- stage: sanitizers
  dependsOn: ["test"]
  pool: "envoy-x64-large"
  jobs:
  - job: sanitizers
    displayName: "do_ci.sh"
    strategy:
      maxParallel: 2
      matrix:
        asan:
          CI_TARGET: "asan"
        tsan:
          CI_TARGET: "tsan"
    timeoutInMinutes: 120
    steps:
    - template: bazel.yml
      parameters:
        ciTarget: $(CI_TARGET)

- stage: coverage_unit
  dependsOn: ["test"]
  pool: "envoy-x64-large"
  jobs:
  - job: coverage_unit
    displayName: "do_ci.sh"
    strategy:
      maxParallel: 1
      matrix:
        coverage:
          CI_TARGET: "coverage"
    timeoutInMinutes: 120
    steps:
    - template: bazel.yml
      parameters:
        ciTarget: $(CI_TARGET)
    - task: PublishPipelineArtifact@1
      condition: always()
      displayName: 'Publish the line coverage report'
      inputs:
        targetPath: $(Build.SourcesDirectory)/coverage_html.zip
        artifactName: UnitTestCoverageReport-$(System.JobAttempt)

# coverage_integration stage was temporarily removed due to the issue
# reported by https://github.com/envoyproxy/nighthawk/issues/1006
- stage: release
  dependsOn:
  - "test_gcc"
  - "sanitizers"
  - "coverage_unit"
  condition: eq(variables['PostSubmit'], true)
  pool: "envoy-x64-large"
  jobs:
  - job: release
    displayName: "do_ci.sh"
    strategy:
      matrix:
        release:
          CI_TARGET: "docker"
    timeoutInMinutes: 120
    steps:
    - template: bazel.yml
      parameters:
        ciTarget: $(CI_TARGET)
